﻿using System;
using System.ComponentModel.Composition;
using System.Data.OracleClient;
using ParaEditor.Infrastructure;
using Micropayment.Model;
using ParaEditor.SysMgr.Service.Entity;
using System.Text;

namespace ParaEditor.SysMgr.Service.RuleMgrService.Impl
{
    [Export(typeof(IRuleMgrService))]
    [PartCreationPolicy(CreationPolicy.Shared)]
    public class RuleMgrServiceImpl : IRuleMgrService
    {
        public Rules GetRules()
        {
            Rules rules = new Rules();
            OracleDataReader dataReader;
            try
            {
                string sqlText = "select RULE_ID,RULE_NAME,MODULE_ID from RULE where RULE_ID <> 0 order by RULE_ID";
                dataReader = DataBase.SqlReader(sqlText);
                while (dataReader.Read())
                {
                    RULE rule = new RULE();
                    rule.RULE_ID = (int)dataReader.GetOracleNumber(0);
                    rule.RULE_NAME = dataReader.GetOracleString(1).ToString();
                    rule.MODULE_ID = dataReader.GetOracleString(2).ToString();
                    rules.Add(rule);
                }
                dataReader.Close();
                dataReader.Dispose();
            }
            catch (Exception ex)
            {
            }
            return rules;
        }

        public Modules GetModuleByID(string id)
        {
            Modules modules = new Modules();
            OracleDataReader dataReader;
            try
            {
                string sqlText = "select  MODULE_SYS,MODULE_NAME,MODULE_ID from MODULE where MODULE_ID IN( " + id + ")";
                dataReader = DataBase.SqlReader(sqlText);
                while (dataReader.Read())
                {
                    MODULE _module = new MODULE();
                    _module.MODULE_SYS = dataReader.GetOracleString(0).ToString();
                    _module.MODULE_NAME = dataReader.GetOracleString(1).ToString();
                    _module.MODULE_ID = (int)dataReader.GetOracleNumber(2);
                    modules.Add(_module);
                }
                dataReader.Close();
                dataReader.Dispose();
            }
            catch (Exception ex)
            { 
            }
            return modules;
        }

        public Modules GetModuleAll()
        {
            Modules modules = new Modules();
            OracleDataReader dataReader;
            try
            {
                string sqlText = "select  MODULE_SYS,MODULE_NAME,MODULE_ID from MODULE";
                dataReader = DataBase.SqlReader(sqlText);
                while (dataReader.Read())
                {
                    MODULE _module = new MODULE();
                    _module.MODULE_SYS = dataReader.GetOracleString(0).ToString();
                    _module.MODULE_NAME = dataReader.GetOracleString(1).ToString();
                    _module.MODULE_ID = (int)dataReader.GetOracleNumber(2);
                    modules.Add(_module);
                }
                dataReader.Close();
                dataReader.Dispose();
            }
            catch (Exception ex)
            {
            }
            return modules;
        }

        public Modules GetModuleWithoutUserdByID(string id)
        {
            Modules modules = new Modules();
            OracleDataReader dataReader;
            try
            {
                string sqlText = "select  MODULE_SYS,MODULE_NAME,MODULE_ID from MODULE where MODULE_ID NOT IN( " + id + ")";
                dataReader = DataBase.SqlReader(sqlText);
                while (dataReader.Read())
                {
                    MODULE _module = new MODULE();
                    _module.MODULE_SYS = dataReader.GetOracleString(0).ToString();
                    _module.MODULE_NAME = dataReader.GetOracleString(1).ToString();
                    _module.MODULE_ID = (int)dataReader.GetOracleNumber(2);
                    modules.Add(_module);
                }
                dataReader.Close();
                dataReader.Dispose();
            }
            catch (Exception ex)
            {
            }
            return modules;
        }

        public bool IsExistRule(int id)
        {
            int count = -1;
            bool flag = false;
            try
            {
                OracleDataReader dataReader;
                string sqlText = "select count(*) from RULE where rule_id = " + id;
                dataReader = DataBase.SqlReader(sqlText);
                while (dataReader.Read())
                {
                    count = (int)dataReader.GetOracleNumber(0);
                    if (count == 1)
                    {
                        flag = true;
                    }
                    else
                    {
                        flag = false;
                    }
                }
            }
            catch (Exception ex)
            { 
            }
            return flag;
        }

        public RULE GetRuleByID(int id)
        {
            RULE rule = new RULE();
            OracleDataReader dataReader;
            string sqlText = "select rule_id,module_id,rule_name from RULE where rule_id = " + id;
            try
            {
                dataReader = DataBase.SqlReader(sqlText);
                while (dataReader.Read())
                {
                    rule.RULE_ID = (int)dataReader.GetOracleNumber(0);
                    rule.MODULE_ID = dataReader.GetOracleString(1).ToString();
                    rule.RULE_NAME = dataReader.GetOracleString(2).ToString();
                }
            }
            catch (Exception ex)
            { 
            }
            return rule;
        }

        public bool CreateRule(RULE rule)
        {
            bool flag = false;
            StringBuilder sb = new StringBuilder("insert into RULE(RULE_ID,RULE_NAME,MODULE_ID) values (");
            try
            {
                sb.Append(rule.RULE_ID + ",");
                sb.Append("'" + rule.RULE_NAME + "',");
                sb.Append("'" + rule.MODULE_ID + "'");
                sb.Append(")");
                DataBase.SqlCommand(sb.ToString());
                sb.Clear();
                flag = true;
            }
            catch (Exception ex)
            {
                flag = false;
            }
            return flag;
        }

        public bool UpdateRuleByID(RULE rule)
        {
            bool flag = false;
            StringBuilder sb = new StringBuilder("update RULE SET ");
            try
            {
                sb.Append(" RULE_NAME = '" + rule.RULE_NAME + "',");
                sb.Append(" MODULE_ID = '" + rule.MODULE_ID + "'");
                sb.Append(" WHERE RULE_ID = " + rule.RULE_ID);
                DataBase.SqlCommand(sb.ToString());
                sb.Clear();
                flag = true;
            }
            catch (Exception ex)
            {
                flag = false;
            }
            return flag;
        }
    }
}
